package org.jeecg.trace.service.impl;

import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.trace.dto.BarCodeTraceDTO;
import org.jeecg.trace.dto.PackageBoxDTO;
import org.jeecg.trace.dto.PackageProductDTO;
import org.jeecg.trace.entity.MesPackItem;
import org.jeecg.trace.entity.MesUnitHistory;
import org.jeecg.trace.entity.MesUnitStatus;
import org.jeecg.trace.mapper.PackageTraceMapper;
import org.jeecg.trace.service.IMesPackItemService;
import org.jeecg.trace.service.IMesUnitHistoryService;
import org.jeecg.trace.service.IMesUnitStatusService;
import org.jeecg.trace.service.PackageTraceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class PackageTraceServiceImpl implements PackageTraceService {
    @Autowired
    private PackageTraceMapper packageTraceMapper;
    @Autowired
    private IMesPackItemService iMesPackItemService;
    @Autowired
    private IMesUnitStatusService iMesUnitStatusService;
    @Autowired
    private IMesUnitHistoryService iMesUnitHistoryService;
    @Override
    public IPage<PackageBoxDTO> listBoxPackage(Page<PackageBoxDTO> page, Map<String, String> map) {
        return packageTraceMapper.listBoxPackage(page,map);
    }

    @Override
    public IPage<PackageProductDTO> listProductPackage(Page<PackageProductDTO> page, Map<String, String> map) {
        return packageTraceMapper.listProductPackage(page,map);
    }

    @Override
    public void unbindPackage(String unitsn,String woid,String partno,int typ,String oldWo) {
        String peek = DynamicDataSourceContextHolder.peek();
        List<String> list = new ArrayList<>();

        //根据产品号查询管体号
//        if (typ>1){
//            List<String> packageProductDTOS = packageTraceMapper.listPackage(unitsn, oldWo);
//            list.addAll(packageProductDTOS);
//        }else{
        BarCodeTraceDTO product =packageTraceMapper.getByCode(unitsn);
        String unitsn1 = product.getUnitsn();
        String line = product.getLine();
        list.add(unitsn1);
//        }

        DynamicDataSourceContextHolder.clear();
        DynamicDataSourceContextHolder.push("business");
        log.info("------修改产品数据开始--------->>DictAspect原数据源{},切换数据源：{}", peek,DynamicDataSourceContextHolder.peek());
        //3 删除所有关联关系
       // list.forEach(item->{
            Wrapper<MesPackItem> wrapper = new QueryWrapper<MesPackItem>().eq("UNITSN", unitsn1);
            iMesPackItemService.remove(wrapper);
        log.info("------删除包装结束--------->>");
//        //4 修改所有产品状态
            iMesUnitStatusService.update(new UpdateWrapper<MesUnitStatus>()
                    .eq("UNITSN",unitsn1)
                    .set("RESULT",2)
                    .set("STATUS",1)
                    .set("WOID",woid)
                    .set("PARTNO",partno)
                    .set("LOTNO",0)
            );
        log.info("------修改status结束--------->>");
//        //6 修改所有产品的所有生产历史工单
            iMesUnitHistoryService.update(new UpdateWrapper<MesUnitHistory>()
                    .eq("UNITSN",unitsn1)
                    .set("WOID",woid)
                    .set("PARTNO",partno));
        log.info("------修改history结束--------->>");
        String substring = woid + unitsn.substring(6);
        packageTraceMapper.updateByCode(unitsn1,substring,line);
        log.info("------修改产品数据结束--------->>");
       // });

    }
}
